<?php
require_once NINA_ROOT . DIRECTORY_SEPARATOR . 'jpgraph' . DIRECTORY_SEPARATOR . 'jpgraph.php';
require_once NINA_ROOT . DIRECTORY_SEPARATOR . 'jpgraph' . DIRECTORY_SEPARATOR . 'jpgraph_bar.php';

/**
 * JPGraph Accumulated Bar Chart Wrapper
 * @package Nina
 * @author John Main <john@thedaddy.org>
 */
class Nina_Graph_Jpgraph_AccBar extends Nina_Graph_Jpgraph_Bar
{
	/**
	 * Constructor
	 * @param integer [$width=400] The width
	 * @param integer [$height=400] The height
	 */
	public function __construct($width = 400, $height = 400) {
		parent::__construct($width, $height);

		$this->plot = new AccBarPlotExtended();
	}

	/**
	 * Set the fill colors
	 * @param array $colors The fill colors
	 */
	public function setFillColors($colors) {
		$this->plot->setFillColors($colors);
	}

	/**
	 * Set the outline colors
	 * @param array $colors The outline colors
	 */
	public function setOutlines($colors) {
		$this->plot->setColors($colors);
	}

	/**
	 * Set the legends
	 * @param array $legends The legends
	 */
	public function setLegends($legends) {
		$this->plot->setLegends($legends);
	}

	/**
	 * Add a plot
	 * @param array $data The data
	 * @param string $legend The legend
	 * @param string $color the outline color
	 * @param string [$fill_color=null] The fill color
	 */
	public function addBarPlot($data, $legend, $color = null, $fill_color = null) {
		$this->plot->addPlot($data, $legend, $color, $fill_color);
	}
}

/**
 * JPGraph Accumulated Bar Chart Extension
 * Adds ability to set data outside of the constructor
 * @package Nina
 * @author John Main <john@thedaddy.org>
 */
class AccBarPlotExtended extends AccBarPlot {

	/**
	 * Parent constructor doesn't work unless we start with some data
	 * @param array [$data=null] The data
	 */
	public function __construct($data = null) {
		if(!$data)
			$data = array(new BarPlotExtended());

		parent::__construct($data);
	}

	/**
	 * Set the fill colors
	 * @param array $colors The fill colors
	 */
	public function setFillColors($colors) {
		foreach($colors as $index => $color) {
			if(isset($this->plots[$index]))
				$this->plots[$index]->SetFillColor($color);
		}
	}

	/**
	 * Set the outline colors
	 * @param array $colors The outline colors
	 */
	public function setColors($colors) {
		foreach($colors as $index => $color) {
			if(isset($this->plots[$index]))
				$this->plots[$index]->SetColor($color);
		}
	}

	/**
	 * Set the legends
	 * @param array $legends The legends
	 */
	public function setLegends($legends) {
		foreach($legends as $index => $legend) {
			if(isset($this->plots[$index]))
				$this->plots[$index]->SetLegend($legend);
		}
	}

	/**
	 * Add a plot
	 * @param array $data The data
	 * @param string $legend The legend
	 * @param string $color the outline color
	 * @param string [$fill_color=null] The fill color
	 */
	public function addPlot($data, $legend, $color, $fill_color = null) {
		if(!$fill_color)
			$fill_color = $color;

		$plot = new BarPlotExtended($data);
		$plot->SetLegend($legend);
		//$plot->SetColor($color);
		$plot->setFillColor($fill_color);

		$this->plots[] = $plot;
		$this->nbrplots = count($this->plots);
	}

	/**
	 * Set the plot data
	 * @param array $data The data
	 */
	public function setData($data) {

		$plots = array();
		if(is_array(reset($data))) {
			foreach($data as $plot_data) {
				$plots[] = new BarPlot($plot_data);
			}
		}
		else
			$plots[] = new BarPlot($data);

		$this->plots = $plots;
		$this->nbrplots = count($plots);

		if( $this->nbrplots < 1 ) {
			JpGraphError::RaiseL(2010);//('Cannot create AccBarPlot from empty plot array.');
		}
		for($i=0; $i < $this->nbrplots; ++$i ) {
			if( empty($this->plots[$i]) || !isset($this->plots[$i]) ) {
				JpGraphError::RaiseL(2011,$i);//("Acc bar plot element nbr $i is undefined or empty.");
			}
		}

		// We can only allow individual plost which do not have specified X-positions
		for($i=0; $i < $this->nbrplots; ++$i ) {
			if( !empty($this->plots[$i]->coords[1]) ) {
				JpGraphError::RaiseL(2015);
				//'Individual bar plots in an AccBarPlot or GroupBarPlot can not have specified X-positions.');
			}
		}

		// Use 0 weight by default which means that the individual bar
		// weights will be used per part n the accumulated bar
		$this->SetWeight(0);

		$this->numpoints = $plots[0]->numpoints;
		$this->value = new DisplayValue();
	}
}
